package ru.jexp.rcp.supply.dnd;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;

import ru.jexp.rcp.supply.viewer.JExpTableViewer;

public class JExpDragSource {

	private DragSource dragSource;

	public JExpDragSource(final JExpTableViewer viewer) {
		dragSource = new DragSource(viewer.getControl(), DND.DROP_COPY);
		dragSource.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		dragSource.addDragListener(new DragSourceAdapter() {
			@Override
			public void dragStart(DragSourceEvent event) {
				if (viewer.getSelection().isEmpty()) {
					event.doit = false;
					event.data = null;
				} else {
					event.data = ((StructuredSelection) viewer.getSelection())
							.getFirstElement();
				}
			}

			@Override
			public void dragSetData(DragSourceEvent event) {
				if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
					event.data = ((StructuredSelection) viewer.getSelection())
							.getFirstElement();
				}
			}
		});
	}

}
